﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;

namespace PDAMain.Forms.InspectionWork
{
    public partial class InspectionSelect : Form
    {
        public InspectionSelect()
        {
            InitializeComponent();
        }

        private void rbPending_Click(object sender, EventArgs e)
        {
            rbProcessed.Checked = false;
        }

        private void rbProcessed_Click(object sender, EventArgs e)
        {
            rbPending.Checked = false;
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //查询
            queryData();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //返回
            InspectionList list = new InspectionList();
            list.Show();
            this.Close();
        }

        private void queryData()
        {
            //类型
            string strType = string.Empty;
            //时间
            string strTime = "1900/01/01 01:00:00";

            if (rbPending.Checked == true)
                strType = "1";
            else if (rbProcessed.Checked == true)
                strType = "9";

            DateTime datenow = DateTime.Now;
            if (cbOneDay.Checked == true)
            {
                strTime = datenow.AddDays(-1).ToString("yyyy/MM/dd hh:mm:ss");
            }
            if (cbThreeDay.Checked == true)
            {
                strTime = datenow.AddDays(-3).ToString("yyyy/MM/dd hh:mm:ss");
            }
            if (cbOneWeek.Checked == true)
            {
                strTime = datenow.AddDays(-7).ToString("yyyy/MM/dd hh:mm:ss");
            }

            DataTable dt = new DataTable();
            DataSet ds = null;
            try
            {
                ds = DataHelper.InspectionWorkDao.select(strType, strTime);
            }
            catch
            {
                ds = DataHelper.InspectionWorkDao.InspectionSelect(strType);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int index = 0; index < ds.Tables[0].Rows.Count; index++)
                    {
                        if (Convert.ToDateTime(ds.Tables[0].Rows[index]["STATUSDATE"]) < Convert.ToDateTime(strTime))
                        {
                            ds.Tables[0].Rows.RemoveAt(index);
                        }
                    }
                }
            }
            //如无数据
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                queryGrid.DataSource = null;
                queryGrid.TableStyles.Clear();
                queryGrid.Refresh();
                MessageBox.Show("无数据");
                return;
            }

            dt = ds.Tables[0];
            for (int k = 0; k < dt.Rows.Count; k++)
            {
                /*
                if (dt.Rows[k]["workStatus"].ToString() == "1")
                {
                    dt.Rows[k]["workStatus"] = "处理中";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "2")
                {
                    dt.Rows[k]["workStatus"] = "转单";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "3")
                {
                    dt.Rows[k]["workStatus"] = "报修";
                }
                else if (dt.Rows[k]["workStatus"].ToString() == "9")
                {
                    dt.Rows[k]["workStatus"] = "已处理";
                }
                else 
                {
                    dt.Rows[k]["workStatus"] = "未定义状态";
                }*/
                if (dt.Rows[k]["status"].ToString() == "1")
                {
                    dt.Rows[k]["status"] = "处理中";
                }
                else if (dt.Rows[k]["status"].ToString() == "2")
                {
                    dt.Rows[k]["status"] = "转单";
                }
                else if (dt.Rows[k]["status"].ToString() == "3")
                {
                    dt.Rows[k]["status"] = "报修";
                }
                else if (dt.Rows[k]["status"].ToString() == "9")
                {
                    dt.Rows[k]["status"] = "已处理";
                }
                else
                {
                    //dt.Rows[k]["status"] = "未定义状态";
                }
            }
            queryGrid.DataSource = dt;
            DataGridTableStyle dts = new DataGridTableStyle();
            dts.MappingName = dt.TableName;

            DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
            col1.HeaderText = "工单编号";
            col1.MappingName = "woNum";
            col1.Width = 80;
            dts.GridColumnStyles.Add(col1);

            DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
            col2.HeaderText = "描述";
            col2.MappingName = "description";
            col2.Width = 84;
            dts.GridColumnStyles.Add(col2);

            DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
            col3.HeaderText = "位置描述";
            col3.MappingName = "locDescription";
            col3.Width = 90;
            dts.GridColumnStyles.Add(col3);

            DataGridTextBoxColumn col4 = new DataGridTextBoxColumn();
            col4.HeaderText = "状态";
            col4.MappingName = "status";
            col4.Width = 60;
            dts.GridColumnStyles.Add(col4);

            queryGrid.TableStyles.Clear();
            queryGrid.TableStyles.Add(dts);
            queryGrid.Refresh();
        }

        private void cbOneDay_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbOneDay.Checked == true)
            {
                cbThreeDay.Checked = false;
                cbOneWeek.Checked = false;
            }
        }

        private void cbThreeDay_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbThreeDay.Checked == true)
            {
                cbOneDay.Checked = false;
                cbOneWeek.Checked = false;
            }
        }

        private void cbOneWeek_CheckStateChanged(object sender, EventArgs e)
        {
            if (cbOneWeek.Checked == true)
            {
                cbOneDay.Checked = false;
                cbThreeDay.Checked = false;
            }
        }

    
        

    }
}